﻿using Microsoft.EntityFrameworkCore;
using Wallpaper.Data.Base;
using Wallpaper.Data.Context;
using Wallpaper.Data.IDal;
using Wallpaper.Models.DTOs.Responses.Tag;
using Wallpaper.Models.Entities;

namespace Wallpaper.Data.Dal
{
    public class TagDal : BaseDal<Tags>, ITagDal
    {
        public TagDal(WallpaperDbContext dbContext) : base(dbContext)
        {
        }


        /// <summary>
        /// 判断标签是否存在
        /// </summary>
        /// <param name="tagName"></param>
        /// <returns></returns>
        public async Task<bool> ExistsByNameAsync(string tagName)
        {
            return await _dbContext.Set<Tags>()
                .AnyAsync(t => t.Name == tagName && !t.IsDeleted);
        }


        public async Task<bool> IsTagUsedAsync(string tagId)
        {
            return await _dbContext.Set<WallpaperTag>()
                .AnyAsync(wt => wt.TagId == tagId);
        }

        /// <summary>
        /// 获取Tags集合
        /// </summary>
        /// <returns></returns>
        public async Task<List<TagResponse>> GetListAsync()
        {
            return await _dbSet
                .OrderBy(t => t.Name)
                .Select(t => new TagResponse
                {
                    Id = t.Id,
                    Name = t.Name,
                    UsageCount=t.UsageCount
                })
                .ToListAsync();
        }
    }
}
